<?
	if (!defined('i-Teck_ADMIN')) die("Hacking attempt");

	switch($mod){
		case "add":			
		?>
			<form name="form1" method="post" action="">
					<article class="module width_full">
						<header><h3>Thêm Danh Mục Sản Phẩm</h3></header>
							<div class="module_content">	
							<fieldset>
										<label>Tên Danh Mục</label>
										<input name="name" type="text" id="name" size="50">
							</fieldset>
							
							<input type="submit" name="cmd" id="cmd" value="Thêm" class="alt_btn">
							</div>
					</article><!-- end of styles article -->
			</form>
			<?
			if(isset($_REQUEST["cmd"])=="Thêm"){
				mysql_query("insert into categories (name, sort) 
							values ('".addslashes($_POST["name"])."',0)");
				mysql_close();
				echo "<meta http-equiv='refresh' content='0; url=?act=categories&mod=view'>";
			}
	break;
	case "view":
			$result=mysql_query("SELECT * FROM categories ORDER BY sort");
			if(@mysql_num_rows($result)<1){echo"<center><font color=red>Chưa có dữ liệu</font></center>";}
			else{
			?>

				<form id="form2" name="form2" method="post" action="">

					<article class="module width_full">
							<header><h3>Quản lý Danh Mục</h3></header>
					
						<div class="tab_container">
							<div id="tab1" class="tab_content">
							<table class="tablesorter" cellspacing="0"> 
							<thead> 
								<tr> 
									<th>STT</th> 
									<th>Tên Danh Mục</th> 
									<th>Sửa</th> 
									<th>Xóa</th>
								</tr> 
							</thead> 
							<tbody> 


					<?  while($r=mysql_fetch_array($result)){?>
								<tr> 
									<td><input name="stt[]" type="text" value="<?=$r['sort'];?>" size="2" /><input name="id[]" type="text" value="<?=$r['id'];?>" style="display:none" /></td> 
									<td><a href="?act=categories&amp;mod=edit&amp;id=<? echo $r['id'];?>"><? echo stripslashes($r['name']);?></a></td> 
									<td><a href="?act=categories&amp;mod=edit&amp;id=<? echo $r['id'];?>">Sửa</a></td> 
									<td><a onclick="return  confirm('Bạn có muốn xóa Danh Mục này ko?');" href="?act=categories&amp;mod=delete&amp;id=<? echo $r['id'];?>">Xóa</a></td> 
								</tr> 
					
					<? }?>
					<tr>
					  <td><input type="submit" name="cmd" id="cmd" value="Sửa thứ tự" class="alt_btn"/></td>
					</tr>

							</tbody> 
							</table></div></div>
						</article><!-- end of styles article -->
				</form>

			<? }
			if(isset($_POST["cmd"])=="Sửa thứ tự"){
					$id=$_POST["id"];
					$stt=$_POST["stt"];
					$lap=count($_POST["id"]);
					for($i=0;$i<$lap;$i++){
					mysql_query("UPDATE categories SET sort=".$stt[$i]." WHERE id=".$id[$i]."");

						}
				
					echo "<meta http-equiv='refresh' content='0; url=?act=categories&mod=view'>";
				}

		break;
		case "edit":
				$id=intval($_REQUEST["id"]);
				$result=mysql_query("select * from categories where id=$id");
				if(mysql_num_rows($result)<=0){echo"<center><font color=red>Chưa có dữ liệu</font</center>";}
				else{
				?>
				<form name="form1" method="post" action="">
				<article class="module width_full">
					<header><h3>Sửa Danh Mục</h3></header>
						<div class="module_content">	
						<fieldset>
									<label>Tên Danh Mục</label>
									<? while($r=mysql_fetch_array($result)){ ?>
									<input name="name" type="text" value="<? echo stripslashes($r["name"]);?>" size="50">
									<? } ?>
						</fieldset>
						
						<input type="submit" name="cmd" id="cmd" value="Sửa" class="alt_btn">
						</div>
				</article><!-- end of styles article -->
				</form>
				
			<? } if(isset($_REQUEST["cmd"])=="Sửa"){
				mysql_query("update categories set name='".addslashes($_POST["name"])."' where id=$id");
				mysql_close();
				echo "<meta http-equiv='refresh' content='0; url=?act=categories&mod=view'>"; 
				}
			
	break;

	case "delete":
		$id=intval($_REQUEST["id"]);
		
		mysql_query("delete from categories where id=$id");
		mysql_close();
		echo "<meta http-equiv='refresh' content='0; url=?act=categories&mod=view'>";
break;

	} 
?>